package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        log.info("进入消费者方法执行了....");
        int  i = 1 / 0;

        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "error.queue"),
            exchange = @Exchange( name = "error.direct"),
            key = {"error"}
    ))
    public void errorQueue(String msg) {
        log.debug("处理失败的消息：【" + msg + "】");

        //TODO 可以根据需要存入数据库，交由人工处理
    }

    /**
     * 定义死信交换机和死信队列
     * @param msg
     */

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dl.ttl.queue"),
            exchange = @Exchange(name = "dl.ttl.exchange"),
            key = {"dl"}
    ))
    public void deadQueue(String msg) {
        log.debug("死信队列的消息：【" + msg + "】");
    }

    /**
     * 定义延迟队列和延迟交换机
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue",durable = "true"),
            exchange = @Exchange(
                    name = "delay.exchange",
                    delayed = "true"
            ),
            key = "delay"
    ))
    public void delayQueue(String msg) {
        log.debug("延迟队列的消息：【" + msg + "】");
    }


    /**
     * 定义惰性队列
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "lazy.queue",
            arguments = @Argument(
                    name = "x-queue-mode",
                    value = "lazy")
    ),
            exchange = @Exchange(name = "lazy.exchange"),
            key = {"lazy"}
    ))
    public void lazyQueue(String msg) {
        log.debug("惰性队列的消息：【" + msg + "】");
    }



}
